// pages/coupon/coupon.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ischecked: false,
        butvalue:-1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that=this;
        var listCar = wx.getStorageSync("listPost");
        var orderCar = [];
        var goodsList = [];
        for (var i in listCar) {
            if (listCar[i].selectStatus == true) {
                orderCar.push(listCar[i])
                var post = {
                    goods_id: listCar[i].id,
                    num: listCar[i].number
                }
                goodsList.push(post);
            }
        }
        that.mycouponTap(goodsList);
    },
    // 获取我的优惠券可使用数据 
    mycouponTap: function (goodslist) {
        var that = this;
        utils.http("/api/v1/order/my_coupon", {
            goods_list: goodslist
        }, function (res) {
            for(var i in res.datas.data_list){
                if (app.globalData.couponId == res.datas.data_list[i].id){
                    that.setData({
                        butvalue: i,
                    })
                }
              
            }
            that.setData({
                couponList: res.datas.data_list
            })
            console.log(that.data.couponList)
        })
    },
    //优惠券返回订单
    radioChange(e) {
        var that=this;
        var index = e.currentTarget.dataset.index;
        that.setData({
            butvalue: index
        })
        app.globalData.couponMoney = that.data.couponList[index].money;
        app.globalData.couponId = that.data.couponList[index].id;
        wx.navigateBack({ 
        })
    },
    radioChange11:function(e){
        var that=this;
        that.setData({
            butvalue: -1
        })
        app.globalData.couponMoney = 0;
        wx.navigateBack({
        })
    }
})